﻿using System;
using System.Collections.Generic;
using System.Linq;
using Autodesk.Revit.DB;
using SectionSignAndFilled.StairsRoomSection.ElementPacket;

namespace SectionSignAndFilled.StairsRoomSection.Kit
{
	internal class LinesMaterial
	{
		public bool Recognition(View viewSec, Document document)
		{
			this.m_lstcurve3sRes = this.divCorssArea(viewSec, document);
			return true;
		}

		private List<List<CurveLoop>> divCorssArea(View viewSec, Document document)
		{
			List<List<Curve>> allLinesByObj = this.GetAllLinesByObj(0);
			List<Curve> list = new List<Curve>();
			this.GetAllLinesByObj(1, ref list);
			List<Curve> list2 = new List<Curve>();
			this.GetAllLinesByObj(2, ref list2);
			List<Curve> list3 = new List<Curve>();
			this.GetAllLinesByObj(3, ref list3);
			List<List<Curve>> collection = new List<List<Curve>>();
			List<List<Curve>> collection2 = new List<List<Curve>>();
			List<List<Curve>> collection3 = new List<List<Curve>>();
			OutSide outSide = new OutSide();
			collection = outSide.Get(ref list, viewSec, document);
			collection2 = outSide.Get(ref list2, viewSec, document);
			collection3 = outSide.Get(ref list3, viewSec, document);
			List<List<Curve>> list4 = outSide.Curve3DToPlane(allLinesByObj);
			list4.AddRange(collection);
			list4.AddRange(collection2);
			list4.AddRange(collection3);
			List<List<Curve>> inputCurves = this.SortlstlstLines(list4);
			new List<List<List<Curve>>>();
			List<List<CurveLoop>> list5 = NTSUtility.MergaeArea(inputCurves);
			for (int i = 0; i < list5.Count; i++)
			{
				List<CurveLoop> list6 = list5[i];
				outSide.PlaneCurveTo3D(ref list6);
			}
			return list5;
		}

		private List<List<Curve>> SortlstlstLines(List<List<Curve>> lstlstCrv)
		{
			List<List<Curve>> list = new List<List<Curve>>();
			foreach (List<Curve> list2 in lstlstCrv)
			{
				LinkedList<Curve> linkedList = new LinkedList<Curve>(list2);
				linkedList.RemoveFirst();
				List<Curve> list3 = new List<Curve>();
				list3.Add(list2[0]);
				this.SortLstLine(ref list3, linkedList);
				list.Add(list3);
				int i = list3.Count<Curve>();
				while (i < list2.Count<Curve>())
				{
					List<Curve> list4 = new List<Curve>();
					list4.Add(linkedList.First<Curve>());
					linkedList.RemoveFirst();
					this.SortLstLine(ref list4, linkedList);
					i += list4.Count<Curve>();
					list.Add(list4);
				}
			}
			return list;
		}

		private void SortLstLine(ref List<Curve> lstCrvSort, LinkedList<Curve> linkedLstCrv)
		{
			for (int i = 0; i < lstCrvSort.Count; i++)
			{
				XYZ endPoint = lstCrvSort[i].GetEndPoint(1);
				foreach (Curve curve in linkedLstCrv)
				{
					XYZ endPoint2 = curve.GetEndPoint(0);
					XYZ endPoint3 = curve.GetEndPoint(1);
					if (endPoint2.IsAlmostEqualTo(endPoint))
					{
						lstCrvSort.Add(curve);
						linkedLstCrv.Remove(curve);
						break;
					}
					if (endPoint3.IsAlmostEqualTo(endPoint))
					{
						lstCrvSort.Add(curve.CreateReversed());
						linkedLstCrv.Remove(curve);
						break;
					}
				}
			}
		}

		private List<List<Curve>> GetAllLinesByObj(int typeObj)
		{
			List<List<Curve>> list = new List<List<Curve>>();
			foreach (ElementObject elementObject in this.m_lstallObjs)
			{
				if (typeObj == 0 && (elementObject.m_objectType == ObjectType.kWallType || elementObject.m_objectType == ObjectType.kWallLayerType))
				{
					list.Add(elementObject.m_lstCutLine);
				}
				else if (typeObj == 1 && (elementObject.m_objectType == ObjectType.kFloorType || elementObject.m_objectType == ObjectType.kFloorLayerType))
				{
					list.Add(elementObject.m_lstCutLine);
				}
				else if (typeObj == 2 && elementObject.m_objectType != ObjectType.kWallType && elementObject.m_objectType != ObjectType.kWallLayerType && elementObject.m_objectType != ObjectType.kFloorType && elementObject.m_objectType != ObjectType.kFloorLayerType)
				{
					list.Add(elementObject.m_lstCutLine);
				}
				else if (typeObj == 3 && (elementObject.m_objectType == ObjectType.kRoofType || elementObject.m_objectType == ObjectType.kRoofType))
				{
					list.Add(elementObject.m_lstCutLine);
				}
			}
			return list;
		}

		private void GetAllLinesByObj(int typeObj, ref List<Curve> curves)
		{
			foreach (ElementObject elementObject in this.m_lstallObjs)
			{
				if (typeObj == 0 && (elementObject.m_objectType == ObjectType.kWallType || elementObject.m_objectType == ObjectType.kWallLayerType))
				{
					curves.AddRange(elementObject.m_lstCutLine);
				}
				else if (typeObj == 1 && (elementObject.m_objectType == ObjectType.kFloorType || elementObject.m_objectType == ObjectType.kFloorLayerType))
				{
					curves.AddRange(elementObject.m_lstCutLine);
				}
				else if (typeObj == 2 && elementObject.m_objectType != ObjectType.kWallType && elementObject.m_objectType != ObjectType.kWallLayerType && elementObject.m_objectType != ObjectType.kFloorType && elementObject.m_objectType != ObjectType.kFloorLayerType)
				{
					curves.AddRange(elementObject.m_lstCutLine);
				}
				else if (typeObj == 3 && (elementObject.m_objectType == ObjectType.kRoofType || elementObject.m_objectType == ObjectType.kRoofType))
				{
					curves.AddRange(elementObject.m_lstCutLine);
				}
			}
		}

		public string m_strFillMaterial;

		public List<List<CurveLoop>> m_lstcurve3sRes = new List<List<CurveLoop>>();

		public List<ElementObject> m_lstallObjs = new List<ElementObject>();
	}
}
